package com.loop.unclelend.ui.activity

import android.content.Context
import android.content.Intent
import android.view.View
import com.loop.unclelend.R
import com.loop.unclelend.base.app.AppStackManager
import com.loop.unclelend.pro.ProActivity
import com.loop.unclelend.ui.fragment.Fm_Pin_Payment_Failed
import com.loop.unclelend.ui.fragment.Fm_Pin_Payment_Process
import com.loop.unclelend.ui.fragment.Fm_Pin_Payment_Success
import com.loop.unclelend.ui.presenter.Pre_Pin_Payment_Status
import com.loop.unclelend.ui.view.IView_Pin_Payment_Status
import com.loop.unclelend.utils.event.UncleEventData
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/27 17:04
 * @Annotation：支付结果处理中
 */
class Act_Pin_Payment_Status: ProActivity(),IView_Pin_Payment_Status {

    var presenter: Pre_Pin_Payment_Status? = null
    companion object {
        private const val INTENT_KEY_ORDER_ID = "orderId"
        private const val INTENT_KEY_THIRD_ORDER_ID = "thirdOrderId"
        fun start(context: Context,orderId: String,thirdOrderId: String) {
            val intent = Intent(context, Act_Pin_Payment_Status::class.java)
            intent.putExtra(INTENT_KEY_ORDER_ID, orderId)
            intent.putExtra(INTENT_KEY_THIRD_ORDER_ID, thirdOrderId)
            context.startActivity(intent)
        }
    }

    override fun getUncleRootView(): View? {
        return null
    }

    override fun initUncleView() {

    }

    override fun initUncleListener() {

    }

    override fun initUncleData() {
        if (presenter != null){
            presenter = Pre_Pin_Payment_Status(this)
        }
        presenter!!.cacheThirdOrderId(getThirdOrderId())
        presenter!!.requestUnclePinPaymentStatusAction()
    }

    /**
     * 原订单id
     */
    override fun getOrderId(): String{
        if (intent.extras == null){
            return ""
        }
        return intent.extras!!.getString(INTENT_KEY_ORDER_ID).toString()
    }

    /**
     * 三方订单id
     */
    private fun getThirdOrderId(): String {
        if (intent.extras == null){
            return ""
        }
        return intent.extras!!.getString(INTENT_KEY_THIRD_ORDER_ID).toString()
    }

    /**
     * 支付成功
     */
    override fun showUnclePinPaymentSuccess(data: JSONObject) {
        val paymentSuccess = Fm_Pin_Payment_Success(this,data)
        replaceContainerFragement(R.id.layout_container,paymentSuccess)
    }

    /**
     * 支付失败
     */
    override fun showUnclePinPaymentFailed(data: JSONObject) {
        val paymentFailedFm = Fm_Pin_Payment_Failed(this,data)
        replaceContainerFragement(R.id.layout_container,paymentFailedFm)
    }

    /**
     * 支付处理中
     */
    override fun showUnclePinPaymentProcess(data: JSONObject) {
        val paymentProcessFm = Fm_Pin_Payment_Process(this,data)
        replaceContainerFragement(R.id.layout_container,paymentProcessFm)
    }

    /**
     * 支付成功 - 跳转到可申请列表
     */
    fun showUncleProductsListAction(){
        publishEventData(UncleEventData(UncleEventData.TYPE.UNCLE_SHOW_HOME))
        AppStackManager.finishAllActivity(Act_Main::class.java)
    }
}